package com.cmbchina.ccd.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cmbchina.ccd.ReportApplicationRunnner;
import com.cmbchina.ccd.db.entity.ReportData;
import com.cmbchina.ccd.db.service.ReportActivityService;
import com.cmbchina.ccd.db.service.ReportDataService;
import com.cmbchina.ccd.enums.ActivityEnum;
import com.cmbchina.ccd.modle.ReportDataTo;
import com.cmbchina.ccd.utils.AnalyserUtil;
import com.cmbchina.ccd.utils.ReportVo;
import com.cmbchina.ccd.utils.ResponseEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
public class DemoController {

    private final static Logger logger = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    ReportApplicationRunnner reportApplicationRunnner;

    @Autowired
    private ReportDataService reportDataService;



    @ResponseBody
    @RequestMapping(value="/index",produces="application/json;charset=UTF-8")
    public String index() {
        logger.info("this is smart report index");
        return "index";
    }

    @ResponseBody
    @RequestMapping(value="/analyse")
    public ResponseEntity analyse(String word) throws IOException {
        List<String> front_words = AnalyserUtil.analyse(word);
        Map<String, String> map = reportApplicationRunnner.map;
        String max_k = findMaxKey(front_words, map);
        if(StringUtils.isBlank(max_k)) {
            return new ResponseEntity<>().success();
        }
        String activityId = map.get(max_k);
        EntityWrapper<ReportData> entityWrapper = new EntityWrapper();
        String dateType = getDateType(word);
        entityWrapper.eq("activity_id", activityId);
        entityWrapper.eq("date_type", dateType);
        List<ReportData> reportDatas = reportDataService.selectList(entityWrapper);
        if(null == reportDatas || reportDatas.size() == 0) {
            return new ResponseEntity<>().success();
        }
        List<ReportDataTo> reportDataTos = transfer(reportDatas);
        Collections.sort(reportDataTos);
        List<ReportVo> reportVos = new ArrayList<>();
        List<String> series = new ArrayList<>();
        reportDatas.forEach(r -> {
            String indexType = ""+r.getIndexType();
            if(!series.contains(indexType)) {
                series.add(indexType);
            }
        });
        for(String serie : series) {
            ReportVo reportVo = new ReportVo();
            reportVo.setTitle(reportApplicationRunnner.activities.get(activityId));
            reportVo.setSeries(ActivityEnum.getNameById(Integer.parseInt(serie)));
            List<String> labels = new ArrayList<>();
            List<String> data = new ArrayList<>();
            for(ReportDataTo reportDataTo : reportDataTos) {
                String indexType = ""+reportDataTo.getIndexType();
                String value = reportDataTo.getValue();
                if(serie.equals(indexType)) {
                    String dateNum = reportDataTo.getDateNum();
                    if(!labels.contains(dateNum)) {
                        labels.add(dateNum);
                    }
                    data.add(value);
                }
            }
            reportVo.setData(data);
            reportVo.setLabels(labels);
            reportVos.add(reportVo);
        }
        return new ResponseEntity().success(reportVos);
    }

    private List<ReportDataTo> transfer(List<ReportData> reportDatas) {
        List<ReportDataTo> list = new ArrayList<>();
        for(ReportData reportData : reportDatas) {
            ReportDataTo reportDataTo = new ReportDataTo();
            reportDataTo.setId(reportData.getId());
            reportDataTo.setActivityId(reportData.getActivityId());
            reportDataTo.setDateNum(reportData.getDateNum());
            reportDataTo.setDateType(reportData.getDateType());
            reportDataTo.setIndexType(reportData.getIndexType());
            reportDataTo.setValue(reportData.getValue());
            list.add(reportDataTo);
        }
        return list;
    }

    /*@ResponseBody
    @RequestMapping(value="/analyse_mock")
    public ResponseEntity analyse_mock(String word) throws IOException {
        ReportVo reportVo = new ReportVo();
        List<String> labels = new ArrayList<>();
        labels.add("202106");
        labels.add("202107");
        labels.add("202108");
        labels.add("202109");
        labels.add("202110");
        List<String> series = new ArrayList<>();
        series.add("月活");
        series.add("金额");
        List<List<String>> data = new ArrayList<>();
        List<String> innerData1 = new ArrayList<>();
        List<String> innerData2 = new ArrayList<>();
        innerData1.add("2000");
        innerData1.add("3000");
        innerData1.add("4000");
        innerData1.add("5000");
        innerData1.add("3000");
        innerData2.add("2000");
        innerData2.add("3000");
        innerData2.add("4000");
        innerData2.add("5000");
        innerData2.add("3000");
        data.add(innerData1);
        data.add(innerData2);
        reportVo.setData(data);
        reportVo.setLabels(labels);
        reportVo.setSeries(series);
        return new ResponseEntity().success(reportVo);
    }
*/
    private String getDateType(String word) {
        if(word.contains("日")) {
            return "1";
        } else if(word.contains("月")) {
            return "2";
        } else if(word.contains("季")) {
            return "3";
        } else if(word.contains("年")) {
            return "4";
        } else {
            return "1";
        }
    }

    private String findMaxKey(List<String> front_words, Map<String, String> map) throws IOException {
        int max_overlay = 0;
        String max_k = null;
        for(String k : map.keySet()) {
            List<String> db_words = AnalyserUtil.analyse(k);
            int overlay = overlay(front_words, db_words);
            if(overlay > max_overlay) {
                max_k = k;
            }
        }
        return max_k;
    }

    private int overlay(List<String> src, List<String> dest) {
        int overlay = 0;
        for(String destStr : dest) {
            if(src.contains(destStr)) {
                overlay++;
            }
        }
        return overlay;
    }
}